<template>
	<view>
		<metabs v-model="tabIndex" :tabs="tabs" :height='92' ></metabs>
		<view class="inpu-form-main">
			<view class="input-item">
				<view class="item-zhang item-form-top" >
					<input  placeholder="输入真实姓名" v-model="detail.name" disabled  />
				</view>
				<view class="item-zhang item-form-top">
						<input type="text" placeholder="输入身份证号"  v-model="detail.id_number" disabled  />
				</view>
			</view>
			<view class="user mt24">
				<view class="text-box">
					<view class="text-box1 flex alcenter ft28 font-bold">
						<view class="icon">
							
						</view>
						<view class="text-box1-text" >
							用户须知
						</view>
					</view>
					<view v-if="userContent" class="mt20" >
						<rich-text :nodes="$fxLink.checkescape2Html(userContent)"></rich-text>
					</view>
					<view v-if="!userContent">
						<view class="text-box2">
							1、请确保<text class='hongse'>实名信息与本人一致</text>	
									如填写错误或使用其他人信息认证导致不能正常提现后果自负
						</view>
						<view class="text-box2">
							2、请确保输入的<text class='heise'>姓名与身份证号匹配</text>			
						</view>
						<view class="text-box2">
							3、<text class='hongse'>未成年</text>
							无法认证本平台，请在监护人协助下进行实名认证。
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="form-inputmadin" @click="realUser">{{tabIndex === 0?'支付宝认证':"微信认证"}}</view>
		<uni-popup ref="addReal">
			<view class="pop-center-main">
				<view class="pop-top-title">
					<view class="title">高级认证</view>
					<image src="@/static/qw@2x.png" @click="closeReal"></image>
				</view>
				<view class="pop_orage">
					{{remark}}
				</view>
				<view class="text-center  mt32">
					<!-- #ifdef APP-PLUS -->
					<image :src="img.replace(/[\r\n]/g,'')" class="add-wx-img" @longpress="ewmApp(1)" show-menu-by-longpress=true></image>
					<!-- #endif -->
					<!-- #ifndef APP-PLUS -->
					<image :src="img.replace(/[\r\n]/g,'')" class="add-wx-img" show-menu-by-longpress=true></image>
					<!-- #endif -->
				</view>
				<view class="add-wx-msg">
					长按识别或保存二维码扫一扫
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import metabs from '@/components/me-tabs/me-tabs.vue'
	import * as userApi from '@/api/user.js'
	import config from '@/config'
	export default{
		data(){
			return{
				tabs: [
					{name:"支付宝"},
					// #ifndef APP-PLUS
					{name:"微信"},
					// #endif
				],
				tabIndex: 0 ,// 当前tab的下标
				detail:"",
				userContent:"",
				img:"",
				remark:""
			}
		},
		components:{
			metabs
		},
		onShow() {
			this.loadDetail2()
		},
		onLoad() {
			this.loadDetail()
			this.getRealNameNotice()
		},
		methods:{
			loadDetail2() {
				let token = uni.getStorageSync("AccessToken")
				userApi.getUserRealStatus({
					wxapp_id: this.$wxapp_id,
					token: token,
					type:2
				})
				.then(result => {
					let isHigh = !!result.data.name
					if(isHigh){
						uni.showToast({
							title:"认证成功",
							icon:"none"
						})
						setTimeout(() => {
							uni.switchTab({
								url:'/pages/my/my'
							})
						},1000)
					}
				})
			},
			loadDetail() {
				let token = uni.getStorageSync("AccessToken")
				userApi.getUserRealStatus({
					wxapp_id: this.$wxapp_id,
					token: token,
					type:1
				})
				.then(result => {
					this.detail = result.data
				})
			},
			getRealNameNotice(){
				userApi.getNotice({
					type:3
				})
				.then(result => {
					this.userContent = result.data.content
				})
			},
			realUser(){
				if(this.tabIndex === 0){
					this.openRenzhengAlready()
				}else{
					this.openRenzhengwxready()
				}
			},
			openRenzhengAlready(){
				
				let token = uni.getStorageSync("AccessToken")
				let _this = this
				userApi.aliAuthentication({
					wxapp_id: this.$wxapp_id,
					token: token,
				}).then(res => {
						if(res.code === 0){
							_this.$toast(res.msg)
							return
						}
						let token_obj = uni.getStorageSync("AccessToken")
						
						let url  = config.apiUrl+"/api/user/startCertify?id="+res.data.id+"&wxapp_id="+this.$wxapp_id+"&token="+token_obj
			
						// #ifdef H5
						let token_yes = token
						location.href = config.apiUrl+"/api/user/startCertify?id="+res.data.id+"&wxapp_id="+this.$wxapp_id+"&token="+token_yes
						// #endif
						// #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-KUAISHOU || MP-ALIPAY
						this.openReal(url,res.data.content)
						// #endif
						// #ifdef APP-PLUS
						plus.runtime.openURL(url,function(res){
							
						})
						// #endif
				}).catch(err => {
					this.$toast(err)
				})
			
				// #ifndef H5
				
				// #endif
			},
			openRenzhengwxready() {
				let token = uni.getStorageSync("AccessToken")
				let _this = this
				userApi.wxAuthentication({
					wxapp_id: this.$wxapp_id,
					token: token
				}).then(res => {
					// #ifdef H5
					let ua = navigator.userAgent.toLowerCase();
					if (ua.match(/MicroMessenger/i) == "micromessenger") { //微信浏览器
						if(res.code === 0){
							_this.$toast(res.msg)
							return
						}
						
						location.href = res.data.url
						
					} else { //普通浏览器
						_this.$toast('请使用微信浏览器打开')
					}
					// #endif
					// #ifdef MP-WEIXIN || MP-BAIDU || MP-ALIPAY
					this.openReal(res.data.url,res.data.content)
					// #endif
				}).catch(err => {
					this.$toast(err)
				})
			},
			openReal(urls,remark){
				this.remark = remark
				let url = encodeURIComponent(urls)
				userApi.getJsonQrcode({
					data:url
				}).then(result => {
					let image = 'data:image/png;base64,' +result.data
					this.img = image
					setTimeout(() => {
						this.$refs.addReal.open('center')
					},500)
					uni.hideLoading()
				})
				
			},
			closeReal(){
				this.$refs.addReal.close()
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: #F2F7FA;
	}
	.add-wx-img{
		width: 80%;
		height: 400rpx;
		border: 1rpx dashed #000;
		padding: 20rpx;
	}
	.add-wx-msg{
		background-color: #FFF5F9;
		color: #333;
		font-weight: 600;
		font-size: 28rpx;
		padding: 16rpx 30rpx;
		text-align: center;
		margin-top: 20rpx;
	}
	.user{
	    margin-bottom: 24rpx;
	     padding: 32rpx;
	     background: #FCFDFD;
	     box-shadow: 0rpx 8rpx 40rpx 0rpx #E8EBEE;
	     border-radius: 0rpx 0rpx 0rpx 0rpx;
	     border: 4rpx solid #FFFFFF;
	     box-sizing: border-box;
	     .user-top{
	         height: 42rpx;
	         font-family: PingFang SC, PingFang SC;
	         font-weight: 600;
	         font-size: 30rpx;
	         color: #333333;
	         line-height: 35rpx;
	         text-align: left;
	         font-style: normal;
	         text-transform: none;
	     }
	     .user-text{
	         font-family: PingFang SC, PingFang SC;
	         font-weight: 400;
	         font-size: 26rpx;
	         color: #333333;
	         line-height: 44rpx;
	         text-align: left;
	         font-style: normal;
	         text-transform: none;
	     }
	}
</style>